package com.west_traveler.recyclerview_adapter;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.west_traveler.entity.entity_category.EntityCategory;
import com.west_traveler.entity.entity_category.Entity_AD_Category;
import com.west_traveler.entity.entity_category.Item_Category;
import com.west_traveler.utils.CommonUtils;
import com.west_traveler.ximalayafm.R;
import com.west_traveler.ximalayafm.category_activity.Category_Activity;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 12/5 0005.
 */

public class CategoryRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private final int TYPE_PAID=0;
    private final int TYPE_GRIDE=1;
    private final int TYPE_AD=2;
    private Context context;
    private List<Item_Category> paidcategorydatas;
    private List<EntityCategory> categorydatas;
    private List<Entity_AD_Category> ADdatas;
    private View view;

    public CategoryRecyclerAdapter(Context context) {
        this.context = context;
        paidcategorydatas = new LinkedList<>();
        categorydatas = new LinkedList<>();
        ADdatas = new LinkedList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TYPE_PAID;
        }else if(position<6){
            return TYPE_GRIDE;
        }else {
            return TYPE_AD;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_PAID:
                view = LayoutInflater.from(context).inflate(R.layout.item_paid_category, parent, false);
                return new ViewHolderPAID(view);
            case TYPE_GRIDE:
                view = LayoutInflater.from(context).inflate(R.layout.item_gride_category, parent, false);
                return new ViewHolderGRIDE(view);

        }
        return null;
    }
    public void addPaid(Item_Category data){

        paidcategorydatas.add(data);
        notifyItemRangeInserted(0,1);
    }
    public void addCategory(List<? extends EntityCategory> datas){
        int size = paidcategorydatas.size() + categorydatas.size();
        categorydatas.addAll(datas);
        notifyItemRangeChanged(size,datas.size());
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case TYPE_PAID:
                if (holder instanceof ViewHolderPAID){
                    Picasso.with(context).load(paidcategorydatas.get(position).getCoverPath()).into(((ViewHolderPAID) holder).iv_paid);
                }
                break;
            case TYPE_GRIDE:
                if (holder instanceof ViewHolderGRIDE){
                    //给gridLayout添加子控件
                    //Log.i("100","给gridLayout添加子控件");
                   // Log.i("100","position......"+position);
                    EntityCategory entityCategory = categorydatas.get(position - 1);
                    final List<Item_Category> list = entityCategory.getList();
                    //Log.i("100","list是否为空"+list.size());
                    final int grideCount = list.size();
                   // Log.i("100","表格数"+grideCount);
                    GridLayout grideLayout = ((ViewHolderGRIDE) holder).gride;
                    grideLayout.removeAllViews();

                        for (int i=0;i<grideCount;i++){
                             final TextView textView = new TextView(context);
                            //text设置图片
                            Target target = new Target() {
                                @Override
                                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                                    Drawable drawable =new BitmapDrawable(bitmap);
                                    //bitmap.getWidth()  bitmap.getHeight()
                                    drawable.setBounds(0, 0,70,70);
                                    textView.setCompoundDrawables(drawable,null,null,null);
                                }
                                @Override
                                public void onBitmapFailed(Drawable errorDrawable) {

                                }

                                @Override
                                public void onPrepareLoad(Drawable placeHolderDrawable) {

                                }
                            };
                            Picasso.with(context).load(list.get(i).getCoverPath()).into(target);
                            textView.setText(list.get(i).getTitle());
                            textView.setPadding(100,20,20,20);
                            textView.setTextSize(15);
                            // textView.setGravity(Gravity.CLIP_HORIZONTAL);
                            final int list_position = i;
                            textView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Item_Category category = list.get(list_position);
                                    int categoryId = category.getId();
                                    String title = category.getTitle();
                                    Intent intent = new Intent();
                                    intent.putExtra("categoryId",categoryId);
                                    intent.putExtra("title",title);
                                    intent.setClass(context,Category_Activity.class);
                                    context.startActivity(intent);
                                }
                            });
                            //控制GridLayout布局
                            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
                            int windowWidth = CommonUtils.getWindowWidth(context);
                            layoutParams.width = windowWidth/2;
                            textView.setLayoutParams(layoutParams);
                            //textview添加到GridLayout
                            grideLayout.addView(textView);
                        }
                }
                break;
            }
    }
    @Override
    public int getItemCount() {
        int i = categorydatas.size()+paidcategorydatas.size()+ADdatas.size();
        //Log.i("100","item总长度..."+i);
        return i;
    }
    /*
    ViewHolderPAID付费页
     */
    public class ViewHolderPAID extends RecyclerView.ViewHolder{
        private final ImageView iv_paid;
        public ViewHolderPAID(View itemView) {
            super(itemView);
            iv_paid = (ImageView) itemView.findViewById(R.id.iv_paid_category);
        }
    }

    /*
    ViewHolderGRIDE分类页
     */
    public class ViewHolderGRIDE extends RecyclerView.ViewHolder{

       private final GridLayout gride;

        public ViewHolderGRIDE(View itemView) {
            super(itemView);
            gride = (GridLayout) itemView.findViewById(R.id.gv_category);

        }
    }
}
